package cn.senjoeson.timedowner.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.senjoeson.timedowner.R;
import cn.senjoeson.timedowner.base.MyBaseAdapter;
import cn.senjoeson.timedowner.bean.HeartList;
import cn.senjoeson.timedowner.dao.DBConfig;
import cn.senjoeson.timedowner.dao.HeartListDao;

/**
 * Created by senjoeson on 2017/12/23.
 */

public class AddHeartTtileActivity extends Activity {
    @BindView(R.id.listview)
    ListView mListview;
    @BindView(R.id.confirm)
    TextView mConfirm;
    @BindView(R.id.input)
    EditText mInput;
    public MyBaseAdapter<HeartList> mAdapter;
    public ArrayList<HeartList> mHeartLists = new ArrayList<>();
    public HeartListDao mHeartListDao;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addhearttitle);
        ButterKnife.bind(this);


        initData();
        initLister();


    }


    private void initData() {
        mHeartListDao=new HeartListDao(this, DBConfig.TABLE_NAME_1);
        List<HeartList> list = mHeartListDao.selectAll();
        mHeartLists.addAll(list);

        mAdapter = new MyBaseAdapter<HeartList>(mHeartLists) {

            @SuppressLint("ViewHolder")
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                HeartList heartList = mDataSets.get(position);
                convertView = View.inflate(parent.getContext(), R.layout.item, null);
                TextView textView = convertView.findViewById(R.id.item_tv);
                textView.setText(heartList.getTitle());
                return convertView;
            }
        };
        mListview.setAdapter(mAdapter);
    }

    private void initLister() {
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = mInput.getText().toString();
                if (TextUtils.isEmpty(string)) {
                    Toast.makeText(AddHeartTtileActivity.this, "请输入一个心愿单,点确定后添加!!", Toast.LENGTH_SHORT).show();
                    return;
                }
                //  mAdapter.addItem(string);
                long data = addData(string);
                Intent intent = new Intent(AddHeartTtileActivity.this, HeartListActivity.class);
                intent.putExtra("titleid", data);
                startActivity(intent);

                //  mInput.setText("");
            }
        });


        mListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                long id1 = mHeartLists.get(position).getId();
                Intent intent = new Intent(AddHeartTtileActivity.this, HeartListActivity.class);
                intent.putExtra("titleid", String.valueOf(id1));
                startActivity(intent);

            }
        });

        mListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                new AlertDialog.Builder(parent.getContext())
                        .setTitle("想要删除该心愿单么?")
                        .setMessage("你想要删除'" + mAdapter.getContent(position).getTitle() + "'这条心愿单么?")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String content1 = mAdapter.getContent(position).getTitle();
                                mAdapter.deleteItem(position);
                                List<HeartList> heartLists = mHeartListDao.selectAll();
                                for (int i = 0; i < heartLists.size(); i++) {
                                    if (TextUtils.equals(heartLists.get(i).getTitle(), content1)) {
                                        long id1 = heartLists.get(i).getId();
                                        mHeartListDao.delete(id1);
                                        break;
                                    }
                                }
                            }
                        })
                        .create().show();
                return true;
            }
        });
    }

    private long addData(String string) {
        Random random = new Random();
        long nextLong = random.nextLong();
       // HeartList heartList = new HeartList(nextLong, string);
        ContentValues contentValues = new ContentValues();
        contentValues.put("titleid",nextLong);
        contentValues.put("content",string);
        mHeartListDao.addOneData(contentValues);
        return nextLong;
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        mHeartLists.clear();
        initData();

    }
}
